import shuffle from './shuffle'

const OPTIONS = 5

const pick = keys => obj => Object.keys(obj).reduce((acc, key) => {
  if(keys.includes(key)) {
    acc[key] = obj[key]
  }
  return acc
}, {})


const sampleOthers = (myid, num, arr) => {
  const filted = arr.filter(({id}) => id !== myid)
  return shuffle(filted).slice(0, num)
}

export const toNamePic = persons => {
  const converted = persons.map(person => {
    const {id, name} = person
    const others = sampleOthers(id, OPTIONS, persons)
    const options = [person, ...others].map(pick(['id', 'pic', 'speech']))
    return {
      title: name,
      answer: id,
      options: shuffle(options)
    }
  })
  return shuffle(converted)
}

export const toNames = persons => persons.map(({id, name}) => ({id, name}))
